//
//  WXSessionShareService.m
//  ShareCenter
//
//  Created by yutao on 14-2-23.
//  Copyright (c) 2014年 yutao. All rights reserved.
//

#import "WXSessionShareService.h"
#import "ShareMessageInfo.h"

@implementation WXSessionShareService

static WXSessionShareService *instance = nil;
+ (id)service
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[WXSessionShareService alloc] init];
    });
    return instance;
}

- (void)registerApplication
{
    [WXApi registerApp:kWeChatAppKey withDescription:@""];
}

- (void)shareMessage:(ShareMessageInfo *)shareMessage
{
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = [self messageObjectWithShareMessage:shareMessage];
    req.scene = WXSceneTimeline;
    [WXApi sendReq:req];
}

- (WXMediaMessage *)messageObjectWithShareMessage:(ShareMessageInfo *) shareMessage
{
    WXMediaMessage *message = [WXMediaMessage message];
    
    if (shareMessage.title.length)
    {
        message.title = shareMessage.title;
    }
    
    if (shareMessage.imageData.length)
    {
        WXImageObject *imageObj = [WXImageObject object];
        imageObj.imageData = shareMessage.imageData;
        message.mediaObject = imageObj;
    }
    
    if (shareMessage.videoSourceUrl.length) {
        WXVideoObject *videoObj = [WXVideoObject object];
        videoObj.videoUrl = shareMessage.videoSourceUrl;
        message.mediaObject = videoObj;
    }
    
    //TODO  支持视频？？？？
    return message;
}

- (BOOL)handleOpenURL:(NSURL *)url
{
    return [WXApi handleOpenURL:url delegate:self];
}


- (void)onResp:(BaseResp *)resp
{
    if (resp.errCode == WXSuccess) {
        
    }
    
    if (resp.errCode == WXErrCodeUserCancel) {
    
    }
    
    if (resp.errCode == WXErrCodeSentFail) {
       
    }
}
@end
